package com.junsi.app.common.util;

import com.alibaba.fastjson.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * http请求
 */
public class HttpClientUtil {

    public static String gotoHttpConnection(String path,String json,String host,String userName,String pwd){
        String result="";
        try {
            URL url=new URL(path);
            HttpURLConnection con= (HttpURLConnection) url.openConnection();
            PrintWriter out=null;
            //设置一些头参数
            con.setRequestMethod("POST");
            con.setRequestProperty("Content-Type","application/json");
            con.setRequestProperty("Accept","application/json");
            con.setRequestProperty("Host",host);
            con.setRequestProperty("username",userName);
            con.setRequestProperty("password",pwd);
            con.setAllowUserInteraction(false);
            con.setDoInput(true);
            con.setDoOutput(true);
            out=new PrintWriter(con.getOutputStream());
            out.print(json);
            out.flush();
            InputStream is=con.getInputStream();
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            String line="";
            while ((line=br.readLine())!=null){
                    result+=line;
            }
            //关闭流
            is.close();
            con.disconnect();
        }catch (Exception e){
            System.out.print("发送POST请求出现异常！"+e);
            e.printStackTrace();
        }
       return result;
    }




    /**
     * 同步上报项目状态
     * @param externalCode 外部项目编号
     * @param btStatus  碧拓系统状态
     * @return
     */
    public  static   String  synchronizedReportingStatus(String  externalCode,String btStatus){
        Map<String,Object>   result = new HashMap<>();
        try{
            //JSONObject parseObject = JSONObject.parseObject(formData);
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            String time=df.format(new Date());
            Map<String,Object> map = new HashMap<>();
            map.put("source","wybt");
            map.put("externalCode",externalCode);
            map.put("btStatus",btStatus);
            map.put("requestTime",time);
            String path = "http://59.37.49.174:4000/fhh.api.open/Handler/GT_WywtUpdateState.ashx";          //请求地址
            //String path = "http://10.187.41.136:9515/Handler/GT_WywtUpdateState.ashx";
            String host = "59.37.49.174:4000";
            String userName="wybt";
            String pwd="wybt@45tgb";
            String  data = HttpClientUtil.gotoHttpConnection(path, JSONObject.toJSONString(map),host,userName,pwd);
            result.put("msg","同步成功！");
            result.put("data",data);
        }catch (Exception e){
            e.printStackTrace();
            result.put("msg","同步出错！");
            result.put("data",null);
        }

        return  result.toString();              //返回输出参数
    }


}
